/*
 * The <code>TBlock</code> class record location of the translated code, and other information of a basic block.
 */
#ifndef TBLOCK_H
#define TBLOCK_H

#include "crossbit/common.h"
#include <vector>
#include "crossbit/GLOBAL.h"

namespace crossbit {

    class TCache; 

    class TBlock {
        public:
            friend class TCache;

            TBlock()
            {
                run_count = GLOBAL::THRESHOLD;
                is_super_block = false;
            } 

            virtual ~TBlock() 
            { 
            } 
            // --------------------------------------------
            //		Basic Management 
            // --------------------------------------------

            // Return size of the target code block 
            XTUint32 getSize() const
            { 
                return tb_size; 
            } 

            // Return the entry address of the target code
            XTMemAddr enterAddr() const
            { 
                return tb_tcode; 
            }

            // Return size of the target code block 
            XTUint32 setSize(XTUint32 size)
            { 
                tb_size = size; 
            }

            // -------------------------------------------------
            // 		    Exit management
            // -------------------------------------------------

            //static const std::string EXITREASON2STR[4];



            // ----------------------------------------------
            //		relocation management
            // ----------------------------------------------

            void addRelocationPoint(XTUint32 where)
            {
                tb_relocations.push_back(where);
            }

            void relocate(XTUint32 offset);

            void setSPC( XTMemAddr spc )
            {
                tb_spc = spc;
            }

            XTMemAddr getSPC()
            {
                return tb_spc;
            }

            //Add by suntingtao for build superblock		
            bool isSupperBlock( void ){ return is_super_block; }
            void setSuperBlock( void ) { is_super_block = true; }
            XTUint32 getRunCount( void ) { return run_count; }
            void  setRunCount( XTUint32 value ) { run_count = value;}
            XTMemAddr getRunCountAddr( void ) { return (XTMemAddr)&run_count;}

        protected:

            // duplicate TBlock is not allowed
            TBlock(const TBlock &tblock) {}
            TBlock& operator=(const TBlock &tblock) { return *this; }
            // the corresponding address in the source binary code of this <code>TBlock</code>
            XTMemAddr   tb_spc;
            // the entry address of the translated code 
            XTMemAddr	tb_tcode;
            // the size the translated code
            XTUint32	tb_size;
            
            std::vector<int> tb_relocations;

            // collect the run time of the of this block to build super block, at the beginning, it is set to a threshold, 
            // and minus one for every execution
            // NOTE : it is only used for basic block 
            XTUint32  run_count;
            // identify if this block is a super block
            bool      is_super_block;
    };

}

#endif
